package cn.sccl.beans;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * @author 饶爽
 * @time 2019年8月31日 下午9:55:06
 * @version 1.0.1
*/
//@Component("user")等同于配置 <bean name="user" class="cn.itcast.bean.User"/>
//@Component("user")
//    @Service("user")// service层
//    @Controller("user")// web层
    @Repository("user")// dao层
//指定对象的作用范围
@Scope(scopeName="singleton")
public class User {
    
    private String name;
    @Value("18")//默认是给value赋值
    private Integer age;
    //@Autowired//自动装配
    //问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.
    //@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象,自动注入辅助添加注入对象
    @Resource(name="car")
    private Car car;
    
    public String getName() {
        return name;
    }
    @Value("Tom")
    public void setName(String name) {
        this.name = name;
    }
    
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    @PostConstruct//在对象被创建后调用.init-method
    public void init(){
        System.out.println("我是初始化方法!");
    }
    @PreDestroy //在销毁之前调用.destory-method
    public void destory(){
        System.out.println("我是销毁方法!");
    }
    
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
    }
}
